/**
 * 生成含有字符的bitmap图片
 * @date   2011-3-27
 * @author tianye
 */
package cn.edu.dlut.WingGame.Graphic;

import java.nio.FloatBuffer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;

public class GLFontBitmap {
	public final static String enCharSet = " `1234567890-=~!@#$%^&*()_+qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM[]\\;\',./{}|:\"<>?";
	public static Canvas canvas = new Canvas();

	public static synchronized void creatFontBitmapEn(GLFont glfont) {

		Paint p = new Paint();
		p.setAntiAlias(true);
		p.setColor(glfont.fontColor.getColor());
		p.setTypeface(glfont.mTypeface);
		p.setTextSize(glfont.fontSize);

		FontMetrics fm = p.getFontMetrics();
		glfont.charHeight = (int) Math.ceil(fm.descent - fm.ascent) + 4;// 获取字符高度
																		// +4为了防止字符之间交错

		glfont.bmpWidth = 512;
		int wCount = (glfont.bmpWidth / glfont.charHeight);
		int hCount = (enCharSet.length() / wCount + 1);
		glfont.bmpHeight = GLTexContainer.pow2(hCount * glfont.charHeight);
		glfont.enCharCount = enCharSet.length();
		glfont.enbitmap = Bitmap.createBitmap(glfont.bmpWidth,
				glfont.bmpHeight, Bitmap.Config.ARGB_8888);
		canvas.setBitmap(glfont.enbitmap);

		char[] c = new char[1];
		float[] pos = new float[2];
		for (int i = 0; i < enCharSet.length(); i++) {
			c[0] = enCharSet.charAt(i);
			GLFontChar chardata = new GLFontChar();
			chardata.charWidth = (int) p.measureText(c, 0, 1);// 获取单个字符的宽度
			float tx = glfont.charHeight * (i % wCount);
			float ty = glfont.charHeight * (i / wCount);
			chardata.coordBuffer = FloatBuffer.wrap(new float[] {
					tx / glfont.bmpWidth, ty / glfont.bmpHeight,
					(tx + glfont.charHeight) / glfont.bmpWidth,
					ty / glfont.bmpHeight, tx / glfont.bmpWidth,
					(ty + glfont.charHeight) / glfont.bmpHeight,
					(tx + glfont.charHeight) / glfont.bmpWidth,
					(ty + glfont.charHeight) / glfont.bmpHeight, });

			pos[0] = tx + 6;
			pos[1] = ty + glfont.charHeight - fm.descent - 4;

			if (chardata.charWidth > glfont.charHeight)
				chardata.charWidth = 0;
			else
				canvas.drawPosText(c, 0, 1, pos, p);

			glfont.enCharTable.put(c[0], chardata);
			chardata = null;
		}
		glfont.isEnUsed = true;

	}

	public static synchronized void creatFontBitmapCn(GLFont glfont, char ch) {
		Paint p = new Paint();
		p.setAntiAlias(true);
		p.setColor(glfont.fontColor.getColor());
		p.setTypeface(glfont.mTypeface);
		p.setTextSize(glfont.fontSize);
		if (!glfont.isCnUsed) {
			FontMetrics fm = p.getFontMetrics();
			glfont.cnDescent = fm.descent;
			glfont.charHeight = (int) Math.ceil(fm.descent - fm.ascent) + 4;// 获取字符高度
																			// +4为了防止字符之间交错
			glfont.cnMax = 512 / glfont.charHeight;
			glfont.isCnUsed = true;
		}

		canvas.setBitmap(glfont.cnbitmap);

		char[] c = new char[1];
		float[] pos = new float[2];

		c[0] = ch;
		GLFontChar chardata = new GLFontChar();
		chardata.charWidth = (int) p.measureText(c, 0, 1);// 获取单个字符的宽度
		float tx = glfont.charHeight * glfont.cnX;
		float ty = glfont.charHeight * glfont.cnY;
		chardata.coordBuffer = FloatBuffer.wrap(new float[] { tx / 512,
				ty / 512, (tx + glfont.charHeight) / 512, ty / 512, tx / 512,
				(ty + glfont.charHeight) / 512, (tx + glfont.charHeight) / 512,
				(ty + glfont.charHeight) / 512, });

		pos[0] = tx + 6;
		pos[1] = ty + glfont.charHeight - glfont.cnDescent - 4;

		if (glfont.cnCharCount >= glfont.cnMax * glfont.cnMax) {

			// canvas.clipRect(tx, ty, tx+glfont.charHeight,
			// ty+glfont.charHeight);
			// glfont.cnCharCount--;
			glfont.cnCharTable.clear();
			// glfont.cnCharTable=null;
			// glfont.cnCharTable=new Hashtable();
			glfont.cnbitmap.recycle();
			glfont.cnbitmap = null;
			glfont.cnbitmap = Bitmap.createBitmap(512, 512,
					Bitmap.Config.ARGB_8888);
			glfont.cnCharCount = 0;
			canvas.setBitmap(glfont.cnbitmap);
		}
		if (chardata.charWidth > glfont.charHeight)
			chardata.charWidth = 0;
		else
			canvas.drawPosText(c, 0, 1, pos, p);

		if (glfont.cnX >= glfont.cnMax - 1) {
			glfont.cnX = 0;

			if (glfont.cnY >= glfont.cnMax - 1)
				glfont.cnY = 0;
			else
				glfont.cnY++;
		} else {
			glfont.cnX++;
		}

		glfont.cnCharTable.put(c[0], chardata);
		glfont.cnCharCount++;
		chardata = null;
	}
}